<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.4.0-alpha - src/liolib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - liolib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.4.0-alpha</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">331</td>
            <td class="headerCovTableEntry">339</td>
            <td class="headerCovTableEntryHi">97.6 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:19</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: liolib.c $
<span class="lineNum">       3 </span>            : ** Standard I/O (and system) library
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #define liolib_c
<span class="lineNum">       8 </span>            : #define LUA_LIB
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;lprefix.h&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #include &lt;ctype.h&gt;
<span class="lineNum">      14 </span>            : #include &lt;errno.h&gt;
<span class="lineNum">      15 </span>            : #include &lt;locale.h&gt;
<span class="lineNum">      16 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">      17 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      18 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      23 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span>            : /*
<span class="lineNum">      29 </span>            : ** Change this macro to accept other modes for 'fopen' besides
<span class="lineNum">      30 </span>            : ** the standard ones.
<span class="lineNum">      31 </span>            : */
<span class="lineNum">      32 </span>            : #if !defined(l_checkmode)
<span class="lineNum">      33 </span>            : 
<span class="lineNum">      34 </span>            : /* accepted extensions to 'mode' in 'fopen' */
<span class="lineNum">      35 </span>            : #if !defined(L_MODEEXT)
<span class="lineNum">      36 </span>            : #define L_MODEEXT       &quot;b&quot;
<span class="lineNum">      37 </span>            : #endif
<span class="lineNum">      38 </span>            : 
<span class="lineNum">      39 </span>            : /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
<span class="lineNum">      40 </span><span class="lineCov">         52 : static int l_checkmode (const char *mode) {</span>
<span class="lineNum">      41 </span><span class="lineCov">         52 :   return (*mode != '\0' &amp;&amp; strchr(&quot;rwa&quot;, *(mode++)) != NULL &amp;&amp;</span>
<span class="lineNum">      42 </span><span class="lineCov">        155 :          (*mode != '+' || ((void)(++mode), 1)) &amp;&amp;  /* skip if char is '+' */</span>
<span class="lineNum">      43 </span><span class="lineCov">         51 :          (strspn(mode, L_MODEEXT) == strlen(mode)));  /* check extensions */</span>
<span class="lineNum">      44 </span>            : }
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            : #endif
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            : /*
<span class="lineNum">      49 </span>            : ** {======================================================
<span class="lineNum">      50 </span>            : ** l_popen spawns a new process connected to the current
<span class="lineNum">      51 </span>            : ** one through the file streams.
<span class="lineNum">      52 </span>            : ** =======================================================
<span class="lineNum">      53 </span>            : */
<span class="lineNum">      54 </span>            : 
<span class="lineNum">      55 </span>            : #if !defined(l_popen)           /* { */
<span class="lineNum">      56 </span>            : 
<span class="lineNum">      57 </span>            : #if defined(LUA_USE_POSIX)      /* { */
<span class="lineNum">      58 </span>            : 
<span class="lineNum">      59 </span>            : #define l_popen(L,c,m)          (fflush(NULL), popen(c,m))
<span class="lineNum">      60 </span>            : #define l_pclose(L,file)        (pclose(file))
<span class="lineNum">      61 </span>            : 
<span class="lineNum">      62 </span>            : #elif defined(LUA_USE_WINDOWS)  /* }{ */
<span class="lineNum">      63 </span>            : 
<span class="lineNum">      64 </span>            : #define l_popen(L,c,m)          (_popen(c,m))
<span class="lineNum">      65 </span>            : #define l_pclose(L,file)        (_pclose(file))
<span class="lineNum">      66 </span>            : 
<span class="lineNum">      67 </span>            : #else                           /* }{ */
<span class="lineNum">      68 </span>            : 
<span class="lineNum">      69 </span>            : /* ISO C definitions */
<span class="lineNum">      70 </span>            : #define l_popen(L,c,m)  \
<span class="lineNum">      71 </span>            :           ((void)c, (void)m, \
<span class="lineNum">      72 </span>            :           luaL_error(L, &quot;'popen' not supported&quot;), \
<span class="lineNum">      73 </span>            :           (FILE*)0)
<span class="lineNum">      74 </span>            : #define l_pclose(L,file)                ((void)L, (void)file, -1)
<span class="lineNum">      75 </span>            : 
<span class="lineNum">      76 </span>            : #endif                          /* } */
<span class="lineNum">      77 </span>            : 
<span class="lineNum">      78 </span>            : #endif                          /* } */
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : /* }====================================================== */
<span class="lineNum">      81 </span>            : 
<span class="lineNum">      82 </span>            : 
<span class="lineNum">      83 </span>            : #if !defined(l_getc)            /* { */
<span class="lineNum">      84 </span>            : 
<span class="lineNum">      85 </span>            : #if defined(LUA_USE_POSIX)
<span class="lineNum">      86 </span>            : #define l_getc(f)               getc_unlocked(f)
<span class="lineNum">      87 </span>            : #define l_lockfile(f)           flockfile(f)
<span class="lineNum">      88 </span>            : #define l_unlockfile(f)         funlockfile(f)
<span class="lineNum">      89 </span>            : #else
<span class="lineNum">      90 </span>            : #define l_getc(f)               getc(f)
<span class="lineNum">      91 </span>            : #define l_lockfile(f)           ((void)0)
<span class="lineNum">      92 </span>            : #define l_unlockfile(f)         ((void)0)
<span class="lineNum">      93 </span>            : #endif
<span class="lineNum">      94 </span>            : 
<span class="lineNum">      95 </span>            : #endif                          /* } */
<span class="lineNum">      96 </span>            : 
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            : /*
<span class="lineNum">      99 </span>            : ** {======================================================
<span class="lineNum">     100 </span>            : ** l_fseek: configuration for longer offsets
<span class="lineNum">     101 </span>            : ** =======================================================
<span class="lineNum">     102 </span>            : */
<span class="lineNum">     103 </span>            : 
<span class="lineNum">     104 </span>            : #if !defined(l_fseek)           /* { */
<span class="lineNum">     105 </span>            : 
<span class="lineNum">     106 </span>            : #if defined(LUA_USE_POSIX)      /* { */
<span class="lineNum">     107 </span>            : 
<span class="lineNum">     108 </span>            : #include &lt;sys/types.h&gt;
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span>            : #define l_fseek(f,o,w)          fseeko(f,o,w)
<span class="lineNum">     111 </span>            : #define l_ftell(f)              ftello(f)
<span class="lineNum">     112 </span>            : #define l_seeknum               off_t
<span class="lineNum">     113 </span>            : 
<span class="lineNum">     114 </span>            : #elif defined(LUA_USE_WINDOWS) &amp;&amp; !defined(_CRTIMP_TYPEINFO) \
<span class="lineNum">     115 </span>            :    &amp;&amp; defined(_MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1400)        /* }{ */
<span class="lineNum">     116 </span>            : 
<span class="lineNum">     117 </span>            : /* Windows (but not DDK) and Visual C++ 2005 or higher */
<span class="lineNum">     118 </span>            : #define l_fseek(f,o,w)          _fseeki64(f,o,w)
<span class="lineNum">     119 </span>            : #define l_ftell(f)              _ftelli64(f)
<span class="lineNum">     120 </span>            : #define l_seeknum               __int64
<span class="lineNum">     121 </span>            : 
<span class="lineNum">     122 </span>            : #else                           /* }{ */
<span class="lineNum">     123 </span>            : 
<span class="lineNum">     124 </span>            : /* ISO C definitions */
<span class="lineNum">     125 </span>            : #define l_fseek(f,o,w)          fseek(f,o,w)
<span class="lineNum">     126 </span>            : #define l_ftell(f)              ftell(f)
<span class="lineNum">     127 </span>            : #define l_seeknum               long
<span class="lineNum">     128 </span>            : 
<span class="lineNum">     129 </span>            : #endif                          /* } */
<span class="lineNum">     130 </span>            : 
<span class="lineNum">     131 </span>            : #endif                          /* } */
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            : /* }====================================================== */
<span class="lineNum">     134 </span>            : 
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span>            : 
<span class="lineNum">     137 </span>            : #define IO_PREFIX       &quot;_IO_&quot;
<span class="lineNum">     138 </span>            : #define IOPREF_LEN      (sizeof(IO_PREFIX)/sizeof(char) - 1)
<span class="lineNum">     139 </span>            : #define IO_INPUT        (IO_PREFIX &quot;input&quot;)
<span class="lineNum">     140 </span>            : #define IO_OUTPUT       (IO_PREFIX &quot;output&quot;)
<span class="lineNum">     141 </span>            : 
<span class="lineNum">     142 </span>            : 
<span class="lineNum">     143 </span>            : typedef luaL_Stream LStream;
<span class="lineNum">     144 </span>            : 
<span class="lineNum">     145 </span>            : 
<span class="lineNum">     146 </span>            : #define tolstream(L)    ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
<span class="lineNum">     147 </span>            : 
<span class="lineNum">     148 </span>            : #define isclosed(p)     ((p)-&gt;closef == NULL)
<span class="lineNum">     149 </span>            : 
<span class="lineNum">     150 </span>            : 
<span class="lineNum">     151 </span><span class="lineCov">          9 : static int io_type (lua_State *L) {</span>
<span class="lineNum">     152 </span>            :   LStream *p;
<span class="lineNum">     153 </span><span class="lineCov">          9 :   luaL_checkany(L, 1);</span>
<span class="lineNum">     154 </span><span class="lineCov">          9 :   p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);</span>
<span class="lineNum">     155 </span><span class="lineCov">          9 :   if (p == NULL)</span>
<span class="lineNum">     156 </span><span class="lineCov">          1 :     lua_pushnil(L);  /* not a file */</span>
<span class="lineNum">     157 </span><span class="lineCov">          8 :   else if (isclosed(p))</span>
<span class="lineNum">     158 </span><span class="lineCov">          2 :     lua_pushliteral(L, &quot;closed file&quot;);</span>
<span class="lineNum">     159 </span>            :   else
<span class="lineNum">     160 </span><span class="lineCov">          6 :     lua_pushliteral(L, &quot;file&quot;);</span>
<span class="lineNum">     161 </span><span class="lineCov">          9 :   return 1;</span>
<span class="lineNum">     162 </span>            : }
<span class="lineNum">     163 </span>            : 
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span><span class="lineCov">          8 : static int f_tostring (lua_State *L) {</span>
<span class="lineNum">     166 </span><span class="lineCov">          8 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     167 </span><span class="lineCov">          8 :   if (isclosed(p))</span>
<span class="lineNum">     168 </span><span class="lineCov">          1 :     lua_pushliteral(L, &quot;file (closed)&quot;);</span>
<span class="lineNum">     169 </span>            :   else
<span class="lineNum">     170 </span><span class="lineCov">          7 :     lua_pushfstring(L, &quot;file (%p)&quot;, p-&gt;f);</span>
<span class="lineNum">     171 </span><span class="lineCov">          8 :   return 1;</span>
<span class="lineNum">     172 </span>            : }
<span class="lineNum">     173 </span>            : 
<span class="lineNum">     174 </span>            : 
<span class="lineNum">     175 </span><span class="lineCov">        260 : static FILE *tofile (lua_State *L) {</span>
<span class="lineNum">     176 </span><span class="lineCov">        260 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     177 </span><span class="lineCov">        260 :   if (isclosed(p))</span>
<span class="lineNum">     178 </span><span class="lineCov">          5 :     luaL_error(L, &quot;attempt to use a closed file&quot;);</span>
<span class="lineNum">     179 </span>            :   lua_assert(p-&gt;f);
<span class="lineNum">     180 </span><span class="lineCov">        255 :   return p-&gt;f;</span>
<span class="lineNum">     181 </span>            : }
<span class="lineNum">     182 </span>            : 
<span class="lineNum">     183 </span>            : 
<span class="lineNum">     184 </span>            : /*
<span class="lineNum">     185 </span>            : ** When creating file handles, always creates a 'closed' file handle
<span class="lineNum">     186 </span>            : ** before opening the actual file; so, if there is a memory error, the
<span class="lineNum">     187 </span>            : ** handle is in a consistent state.
<span class="lineNum">     188 </span>            : */
<span class="lineNum">     189 </span><span class="lineCov">        385 : static LStream *newprefile (lua_State *L) {</span>
<span class="lineNum">     190 </span><span class="lineCov">        385 :   LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0);</span>
<span class="lineNum">     191 </span><span class="lineCov">        385 :   p-&gt;closef = NULL;  /* mark file handle as 'closed' */</span>
<span class="lineNum">     192 </span><span class="lineCov">        385 :   luaL_setmetatable(L, LUA_FILEHANDLE);</span>
<span class="lineNum">     193 </span><span class="lineCov">        385 :   return p;</span>
<span class="lineNum">     194 </span>            : }
<span class="lineNum">     195 </span>            : 
<span class="lineNum">     196 </span>            : 
<span class="lineNum">     197 </span>            : /*
<span class="lineNum">     198 </span>            : ** Calls the 'close' function from a file handle. The 'volatile' avoids
<span class="lineNum">     199 </span>            : ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
<span class="lineNum">     200 </span>            : ** 32 bits).
<span class="lineNum">     201 </span>            : */
<span class="lineNum">     202 </span><span class="lineCov">        357 : static int aux_close (lua_State *L) {</span>
<span class="lineNum">     203 </span><span class="lineCov">        357 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     204 </span><span class="lineCov">        357 :   volatile lua_CFunction cf = p-&gt;closef;</span>
<span class="lineNum">     205 </span><span class="lineCov">        357 :   p-&gt;closef = NULL;  /* mark stream as closed */</span>
<span class="lineNum">     206 </span><span class="lineCov">        357 :   return (*cf)(L);  /* close it */</span>
<span class="lineNum">     207 </span>            : }
<span class="lineNum">     208 </span>            : 
<span class="lineNum">     209 </span>            : 
<span class="lineNum">     210 </span><span class="lineCov">        110 : static int f_close (lua_State *L) {</span>
<span class="lineNum">     211 </span><span class="lineCov">        110 :   tofile(L);  /* make sure argument is an open stream */</span>
<span class="lineNum">     212 </span><span class="lineCov">        109 :   return aux_close(L);</span>
<span class="lineNum">     213 </span>            : }
<span class="lineNum">     214 </span>            : 
<span class="lineNum">     215 </span>            : 
<span class="lineNum">     216 </span><span class="lineCov">          6 : static int io_close (lua_State *L) {</span>
<span class="lineNum">     217 </span><span class="lineCov">          6 :   if (lua_isnone(L, 1))  /* no argument? */</span>
<span class="lineNum">     218 </span><span class="lineNoCov">          0 :     lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT);  /* use standard output */</span>
<span class="lineNum">     219 </span><span class="lineCov">          6 :   return f_close(L);</span>
<span class="lineNum">     220 </span>            : }
<span class="lineNum">     221 </span>            : 
<span class="lineNum">     222 </span>            : 
<span class="lineNum">     223 </span><span class="lineCov">        359 : static int f_gc (lua_State *L) {</span>
<span class="lineNum">     224 </span><span class="lineCov">        359 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     225 </span><span class="lineCov">        359 :   if (!isclosed(p) &amp;&amp; p-&gt;f != NULL)</span>
<span class="lineNum">     226 </span><span class="lineCov">        247 :     aux_close(L);  /* ignore closed and incompletely open files */</span>
<span class="lineNum">     227 </span><span class="lineCov">        359 :   return 0;</span>
<span class="lineNum">     228 </span>            : }
<span class="lineNum">     229 </span>            : 
<span class="lineNum">     230 </span>            : 
<span class="lineNum">     231 </span>            : /*
<span class="lineNum">     232 </span>            : ** function to close regular files
<span class="lineNum">     233 </span>            : */
<span class="lineNum">     234 </span><span class="lineCov">         55 : static int io_fclose (lua_State *L) {</span>
<span class="lineNum">     235 </span><span class="lineCov">         55 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     236 </span><span class="lineCov">         55 :   int res = fclose(p-&gt;f);</span>
<span class="lineNum">     237 </span><span class="lineCov">         55 :   return luaL_fileresult(L, (res == 0), NULL);</span>
<span class="lineNum">     238 </span>            : }
<span class="lineNum">     239 </span>            : 
<span class="lineNum">     240 </span>            : 
<span class="lineNum">     241 </span><span class="lineCov">         58 : static LStream *newfile (lua_State *L) {</span>
<span class="lineNum">     242 </span><span class="lineCov">         58 :   LStream *p = newprefile(L);</span>
<span class="lineNum">     243 </span><span class="lineCov">         58 :   p-&gt;f = NULL;</span>
<span class="lineNum">     244 </span><span class="lineCov">         58 :   p-&gt;closef = &amp;io_fclose;</span>
<span class="lineNum">     245 </span><span class="lineCov">         58 :   return p;</span>
<span class="lineNum">     246 </span>            : }
<span class="lineNum">     247 </span>            : 
<span class="lineNum">     248 </span>            : 
<span class="lineNum">     249 </span><span class="lineCov">          4 : static void opencheck (lua_State *L, const char *fname, const char *mode) {</span>
<span class="lineNum">     250 </span><span class="lineCov">          4 :   LStream *p = newfile(L);</span>
<span class="lineNum">     251 </span><span class="lineCov">          4 :   p-&gt;f = fopen(fname, mode);</span>
<span class="lineNum">     252 </span><span class="lineCov">          4 :   if (p-&gt;f == NULL)</span>
<span class="lineNum">     253 </span><span class="lineCov">          1 :     luaL_error(L, &quot;cannot open file '%s' (%s)&quot;, fname, strerror(errno));</span>
<span class="lineNum">     254 </span><span class="lineCov">          3 : }</span>
<span class="lineNum">     255 </span>            : 
<span class="lineNum">     256 </span>            : 
<span class="lineNum">     257 </span><span class="lineCov">         52 : static int io_open (lua_State *L) {</span>
<span class="lineNum">     258 </span><span class="lineCov">         52 :   const char *filename = luaL_checkstring(L, 1);</span>
<span class="lineNum">     259 </span><span class="lineCov">         52 :   const char *mode = luaL_optstring(L, 2, &quot;r&quot;);</span>
<span class="lineNum">     260 </span><span class="lineCov">         52 :   LStream *p = newfile(L);</span>
<span class="lineNum">     261 </span><span class="lineCov">         52 :   const char *md = mode;  /* to traverse/check mode */</span>
<span class="lineNum">     262 </span><span class="lineCov">         52 :   luaL_argcheck(L, l_checkmode(md), 2, &quot;invalid mode&quot;);</span>
<span class="lineNum">     263 </span><span class="lineCov">         51 :   p-&gt;f = fopen(filename, mode);</span>
<span class="lineNum">     264 </span><span class="lineCov">         51 :   return (p-&gt;f == NULL) ? luaL_fileresult(L, 0, filename) : 1;</span>
<span class="lineNum">     265 </span>            : }
<span class="lineNum">     266 </span>            : 
<span class="lineNum">     267 </span>            : 
<span class="lineNum">     268 </span>            : /*
<span class="lineNum">     269 </span>            : ** function to close 'popen' files
<span class="lineNum">     270 </span>            : */
<span class="lineNum">     271 </span><span class="lineCov">         60 : static int io_pclose (lua_State *L) {</span>
<span class="lineNum">     272 </span><span class="lineCov">         60 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     273 </span><span class="lineCov">         60 :   return luaL_execresult(L, l_pclose(L, p-&gt;f));</span>
<span class="lineNum">     274 </span>            : }
<span class="lineNum">     275 </span>            : 
<span class="lineNum">     276 </span>            : 
<span class="lineNum">     277 </span><span class="lineCov">         60 : static int io_popen (lua_State *L) {</span>
<span class="lineNum">     278 </span><span class="lineCov">         60 :   const char *filename = luaL_checkstring(L, 1);</span>
<span class="lineNum">     279 </span><span class="lineCov">         60 :   const char *mode = luaL_optstring(L, 2, &quot;r&quot;);</span>
<span class="lineNum">     280 </span><span class="lineCov">         60 :   LStream *p = newprefile(L);</span>
<span class="lineNum">     281 </span><span class="lineCov">         60 :   p-&gt;f = l_popen(L, filename, mode);</span>
<span class="lineNum">     282 </span><span class="lineCov">         60 :   p-&gt;closef = &amp;io_pclose;</span>
<span class="lineNum">     283 </span><span class="lineCov">         60 :   return (p-&gt;f == NULL) ? luaL_fileresult(L, 0, filename) : 1;</span>
<span class="lineNum">     284 </span>            : }
<span class="lineNum">     285 </span>            : 
<span class="lineNum">     286 </span>            : 
<span class="lineNum">     287 </span><span class="lineCov">          2 : static int io_tmpfile (lua_State *L) {</span>
<span class="lineNum">     288 </span><span class="lineCov">          2 :   LStream *p = newfile(L);</span>
<span class="lineNum">     289 </span><span class="lineCov">          2 :   p-&gt;f = tmpfile();</span>
<span class="lineNum">     290 </span><span class="lineCov">          2 :   return (p-&gt;f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;</span>
<span class="lineNum">     291 </span>            : }
<span class="lineNum">     292 </span>            : 
<span class="lineNum">     293 </span>            : 
<span class="lineNum">     294 </span><span class="lineCov">          8 : static FILE *getiofile (lua_State *L, const char *findex) {</span>
<span class="lineNum">     295 </span>            :   LStream *p;
<span class="lineNum">     296 </span><span class="lineCov">          8 :   lua_getfield(L, LUA_REGISTRYINDEX, findex);</span>
<span class="lineNum">     297 </span><span class="lineCov">          8 :   p = (LStream *)lua_touserdata(L, -1);</span>
<span class="lineNum">     298 </span><span class="lineCov">          8 :   if (isclosed(p))</span>
<span class="lineNum">     299 </span><span class="lineNoCov">          0 :     luaL_error(L, &quot;standard %s file is closed&quot;, findex + IOPREF_LEN);</span>
<span class="lineNum">     300 </span><span class="lineCov">          8 :   return p-&gt;f;</span>
<span class="lineNum">     301 </span>            : }
<span class="lineNum">     302 </span>            : 
<span class="lineNum">     303 </span>            : 
<span class="lineNum">     304 </span><span class="lineCov">          8 : static int g_iofile (lua_State *L, const char *f, const char *mode) {</span>
<span class="lineNum">     305 </span><span class="lineCov">          8 :   if (!lua_isnoneornil(L, 1)) {</span>
<span class="lineNum">     306 </span><span class="lineCov">          4 :     const char *filename = lua_tostring(L, 1);</span>
<span class="lineNum">     307 </span><span class="lineCov">          4 :     if (filename)</span>
<span class="lineNum">     308 </span><span class="lineCov">          2 :       opencheck(L, filename, mode);</span>
<span class="lineNum">     309 </span>            :     else {
<span class="lineNum">     310 </span><span class="lineCov">          2 :       tofile(L);  /* check that it's a valid file handle */</span>
<span class="lineNum">     311 </span><span class="lineCov">          2 :       lua_pushvalue(L, 1);</span>
<span class="lineNum">     312 </span>            :     }
<span class="lineNum">     313 </span><span class="lineCov">          4 :     lua_setfield(L, LUA_REGISTRYINDEX, f);</span>
<span class="lineNum">     314 </span>            :   }
<span class="lineNum">     315 </span>            :   /* return current value */
<span class="lineNum">     316 </span><span class="lineCov">          8 :   lua_getfield(L, LUA_REGISTRYINDEX, f);</span>
<span class="lineNum">     317 </span><span class="lineCov">          8 :   return 1;</span>
<span class="lineNum">     318 </span>            : }
<span class="lineNum">     319 </span>            : 
<span class="lineNum">     320 </span>            : 
<span class="lineNum">     321 </span><span class="lineCov">          4 : static int io_input (lua_State *L) {</span>
<span class="lineNum">     322 </span><span class="lineCov">          4 :   return g_iofile(L, IO_INPUT, &quot;r&quot;);</span>
<span class="lineNum">     323 </span>            : }
<span class="lineNum">     324 </span>            : 
<span class="lineNum">     325 </span>            : 
<span class="lineNum">     326 </span><span class="lineCov">          4 : static int io_output (lua_State *L) {</span>
<span class="lineNum">     327 </span><span class="lineCov">          4 :   return g_iofile(L, IO_OUTPUT, &quot;w&quot;);</span>
<span class="lineNum">     328 </span>            : }
<span class="lineNum">     329 </span>            : 
<span class="lineNum">     330 </span>            : 
<span class="lineNum">     331 </span>            : static int io_readline (lua_State *L);
<span class="lineNum">     332 </span>            : 
<span class="lineNum">     333 </span>            : 
<span class="lineNum">     334 </span>            : /*
<span class="lineNum">     335 </span>            : ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit
<span class="lineNum">     336 </span>            : ** in the limit for upvalues of a closure)
<span class="lineNum">     337 </span>            : */
<span class="lineNum">     338 </span>            : #define MAXARGLINE      250
<span class="lineNum">     339 </span>            : 
<span class="lineNum">     340 </span>            : /*
<span class="lineNum">     341 </span>            : ** Auxiliar function to create the iteration function for 'lines'.
<span class="lineNum">     342 </span>            : ** The iteration function is a closure over 'io_readline', with
<span class="lineNum">     343 </span>            : ** the following upvalues:
<span class="lineNum">     344 </span>            : ** 1) The file being read (first value in the stack)
<span class="lineNum">     345 </span>            : ** 2) the number of arguments to read
<span class="lineNum">     346 </span>            : ** 3) a boolean, true iff file has to be closed when finished ('toclose')
<span class="lineNum">     347 </span>            : ** *) a variable number of format arguments (rest of the stack)
<span class="lineNum">     348 </span>            : */
<span class="lineNum">     349 </span><span class="lineCov">          8 : static void aux_lines (lua_State *L, int toclose) {</span>
<span class="lineNum">     350 </span><span class="lineCov">          8 :   int n = lua_gettop(L) - 1;  /* number of arguments to read */</span>
<span class="lineNum">     351 </span><span class="lineCov">          8 :   luaL_argcheck(L, n &lt;= MAXARGLINE, MAXARGLINE + 2, &quot;too many arguments&quot;);</span>
<span class="lineNum">     352 </span><span class="lineCov">          8 :   lua_pushvalue(L, 1);  /* file */</span>
<span class="lineNum">     353 </span><span class="lineCov">          8 :   lua_pushinteger(L, n);  /* number of arguments to read */</span>
<span class="lineNum">     354 </span><span class="lineCov">          8 :   lua_pushboolean(L, toclose);  /* close/not close file when finished */</span>
<span class="lineNum">     355 </span><span class="lineCov">          8 :   lua_rotate(L, 2, 3);  /* move the three values to their positions */</span>
<span class="lineNum">     356 </span><span class="lineCov">          8 :   lua_pushcclosure(L, io_readline, 3 + n);</span>
<span class="lineNum">     357 </span><span class="lineCov">          8 : }</span>
<span class="lineNum">     358 </span>            : 
<span class="lineNum">     359 </span>            : 
<span class="lineNum">     360 </span><span class="lineCov">          5 : static int f_lines (lua_State *L) {</span>
<span class="lineNum">     361 </span><span class="lineCov">          5 :   tofile(L);  /* check that it's a valid file handle */</span>
<span class="lineNum">     362 </span><span class="lineCov">          5 :   aux_lines(L, 0);</span>
<span class="lineNum">     363 </span><span class="lineCov">          5 :   return 1;</span>
<span class="lineNum">     364 </span>            : }
<span class="lineNum">     365 </span>            : 
<span class="lineNum">     366 </span>            : 
<span class="lineNum">     367 </span>            : /*
<span class="lineNum">     368 </span>            : ** Return an iteration function for 'io.lines'. If file has to be
<span class="lineNum">     369 </span>            : ** closed, also returns the file itself as a second result (to be
<span class="lineNum">     370 </span>            : ** closed as the state at the exit of a generic for).
<span class="lineNum">     371 </span>            : */
<span class="lineNum">     372 </span><span class="lineCov">          4 : static int io_lines (lua_State *L) {</span>
<span class="lineNum">     373 </span>            :   int toclose;
<span class="lineNum">     374 </span><span class="lineCov">          4 :   if (lua_isnone(L, 1)) lua_pushnil(L);  /* at least one argument */</span>
<span class="lineNum">     375 </span><span class="lineCov">          4 :   if (lua_isnil(L, 1)) {  /* no file name? */</span>
<span class="lineNum">     376 </span><span class="lineCov">          2 :     lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT);  /* get default input */</span>
<span class="lineNum">     377 </span><span class="lineCov">          2 :     lua_replace(L, 1);  /* put it at index 1 */</span>
<span class="lineNum">     378 </span><span class="lineCov">          2 :     tofile(L);  /* check that it's a valid file handle */</span>
<span class="lineNum">     379 </span><span class="lineCov">          2 :     toclose = 0;  /* do not close it after iteration */</span>
<span class="lineNum">     380 </span>            :   }
<span class="lineNum">     381 </span>            :   else {  /* open a new file */
<span class="lineNum">     382 </span><span class="lineCov">          2 :     const char *filename = luaL_checkstring(L, 1);</span>
<span class="lineNum">     383 </span><span class="lineCov">          2 :     opencheck(L, filename, &quot;r&quot;);</span>
<span class="lineNum">     384 </span><span class="lineCov">          1 :     lua_replace(L, 1);  /* put file at index 1 */</span>
<span class="lineNum">     385 </span><span class="lineCov">          1 :     toclose = 1;  /* close it after iteration */</span>
<span class="lineNum">     386 </span>            :   }
<span class="lineNum">     387 </span><span class="lineCov">          3 :   aux_lines(L, toclose);  /* push iteration function */</span>
<span class="lineNum">     388 </span><span class="lineCov">          3 :   if (toclose) {</span>
<span class="lineNum">     389 </span><span class="lineCov">          1 :     lua_pushnil(L);  /* state */</span>
<span class="lineNum">     390 </span><span class="lineCov">          1 :     lua_pushnil(L);  /* control */</span>
<span class="lineNum">     391 </span><span class="lineCov">          1 :     lua_pushvalue(L, 1);  /* file is the to-be-closed variable (4th result) */</span>
<span class="lineNum">     392 </span><span class="lineCov">          1 :     return 4;</span>
<span class="lineNum">     393 </span>            :   }
<span class="lineNum">     394 </span>            :   else
<span class="lineNum">     395 </span><span class="lineCov">          2 :     return 1;</span>
<span class="lineNum">     396 </span>            : }
<span class="lineNum">     397 </span>            : 
<span class="lineNum">     398 </span>            : 
<span class="lineNum">     399 </span>            : /*
<span class="lineNum">     400 </span>            : ** {======================================================
<span class="lineNum">     401 </span>            : ** READ
<span class="lineNum">     402 </span>            : ** =======================================================
<span class="lineNum">     403 </span>            : */
<span class="lineNum">     404 </span>            : 
<span class="lineNum">     405 </span>            : 
<span class="lineNum">     406 </span>            : /* maximum length of a numeral */
<span class="lineNum">     407 </span>            : #if !defined (L_MAXLENNUM)
<span class="lineNum">     408 </span>            : #define L_MAXLENNUM     200
<span class="lineNum">     409 </span>            : #endif
<span class="lineNum">     410 </span>            : 
<span class="lineNum">     411 </span>            : 
<span class="lineNum">     412 </span>            : /* auxiliary structure used by 'read_number' */
<span class="lineNum">     413 </span>            : typedef struct {
<span class="lineNum">     414 </span>            :   FILE *f;  /* file being read */
<span class="lineNum">     415 </span>            :   int c;  /* current character (look ahead) */
<span class="lineNum">     416 </span>            :   int n;  /* number of elements in buffer 'buff' */
<span class="lineNum">     417 </span>            :   char buff[L_MAXLENNUM + 1];  /* +1 for ending '\0' */
<span class="lineNum">     418 </span>            : } RN;
<span class="lineNum">     419 </span>            : 
<span class="lineNum">     420 </span>            : 
<span class="lineNum">     421 </span>            : /*
<span class="lineNum">     422 </span>            : ** Add current char to buffer (if not out of space) and read next one
<span class="lineNum">     423 </span>            : */
<span class="lineNum">     424 </span><span class="lineCov">        231 : static int nextc (RN *rn) {</span>
<span class="lineNum">     425 </span><span class="lineCov">        231 :   if (rn-&gt;n &gt;= L_MAXLENNUM) {  /* buffer overflow? */</span>
<span class="lineNum">     426 </span><span class="lineCov">          1 :     rn-&gt;buff[0] = '\0';  /* invalidate result */</span>
<span class="lineNum">     427 </span><span class="lineCov">          1 :     return 0;  /* fail */</span>
<span class="lineNum">     428 </span>            :   }
<span class="lineNum">     429 </span>            :   else {
<span class="lineNum">     430 </span><span class="lineCov">        230 :     rn-&gt;buff[rn-&gt;n++] = rn-&gt;c;  /* save current char */</span>
<span class="lineNum">     431 </span><span class="lineCov">        230 :     rn-&gt;c = l_getc(rn-&gt;f);  /* read next one */</span>
<span class="lineNum">     432 </span><span class="lineCov">        230 :     return 1;</span>
<span class="lineNum">     433 </span>            :   }
<span class="lineNum">     434 </span>            : }
<span class="lineNum">     435 </span>            : 
<span class="lineNum">     436 </span>            : 
<span class="lineNum">     437 </span>            : /*
<span class="lineNum">     438 </span>            : ** Accept current char if it is in 'set' (of size 2)
<span class="lineNum">     439 </span>            : */
<span class="lineNum">     440 </span><span class="lineCov">         45 : static int test2 (RN *rn, const char *set) {</span>
<span class="lineNum">     441 </span><span class="lineCov">         45 :   if (rn-&gt;c == set[0] || rn-&gt;c == set[1])</span>
<span class="lineNum">     442 </span><span class="lineCov">          8 :     return nextc(rn);</span>
<span class="lineNum">     443 </span><span class="lineCov">         37 :   else return 0;</span>
<span class="lineNum">     444 </span>            : }
<span class="lineNum">     445 </span>            : 
<span class="lineNum">     446 </span>            : 
<span class="lineNum">     447 </span>            : /*
<span class="lineNum">     448 </span>            : ** Read a sequence of (hex)digits
<span class="lineNum">     449 </span>            : */
<span class="lineNum">     450 </span><span class="lineCov">         15 : static int readdigits (RN *rn, int hex) {</span>
<span class="lineNum">     451 </span><span class="lineCov">         15 :   int count = 0;</span>
<span class="lineNum">     452 </span><span class="lineCov">        237 :   while ((hex ? isxdigit(rn-&gt;c) : isdigit(rn-&gt;c)) &amp;&amp; nextc(rn))</span>
<span class="lineNum">     453 </span><span class="lineCov">        222 :     count++;</span>
<span class="lineNum">     454 </span><span class="lineCov">         15 :   return count;</span>
<span class="lineNum">     455 </span>            : }
<span class="lineNum">     456 </span>            : 
<span class="lineNum">     457 </span>            : 
<span class="lineNum">     458 </span>            : /*
<span class="lineNum">     459 </span>            : ** Read a number: first reads a valid prefix of a numeral into a buffer.
<span class="lineNum">     460 </span>            : ** Then it calls 'lua_stringtonumber' to check whether the format is
<span class="lineNum">     461 </span>            : ** correct and to convert it to a Lua number.
<span class="lineNum">     462 </span>            : */
<span class="lineNum">     463 </span><span class="lineCov">         11 : static int read_number (lua_State *L, FILE *f) {</span>
<span class="lineNum">     464 </span>            :   RN rn;
<span class="lineNum">     465 </span><span class="lineCov">         11 :   int count = 0;</span>
<span class="lineNum">     466 </span><span class="lineCov">         11 :   int hex = 0;</span>
<span class="lineNum">     467 </span>            :   char decp[2];
<span class="lineNum">     468 </span><span class="lineCov">         11 :   rn.f = f; rn.n = 0;</span>
<span class="lineNum">     469 </span><span class="lineCov">         11 :   decp[0] = lua_getlocaledecpoint();  /* get decimal point from locale */</span>
<span class="lineNum">     470 </span><span class="lineCov">         11 :   decp[1] = '.';  /* always accept a dot */</span>
<span class="lineNum">     471 </span><span class="lineCov">         11 :   l_lockfile(rn.f);</span>
<span class="lineNum">     472 </span><span class="lineCov">         33 :   do { rn.c = l_getc(rn.f); } while (isspace(rn.c));  /* skip spaces */</span>
<span class="lineNum">     473 </span><span class="lineCov">         11 :   test2(&amp;rn, &quot;-+&quot;);  /* optional sign */</span>
<span class="lineNum">     474 </span><span class="lineCov">         11 :   if (test2(&amp;rn, &quot;00&quot;)) {</span>
<span class="lineNum">     475 </span><span class="lineCov">          2 :     if (test2(&amp;rn, &quot;xX&quot;)) hex = 1;  /* numeral is hexadecimal */</span>
<span class="lineNum">     476 </span><span class="lineCov">          1 :     else count = 1;  /* count initial '0' as a valid digit */</span>
<span class="lineNum">     477 </span>            :   }
<span class="lineNum">     478 </span><span class="lineCov">         11 :   count += readdigits(&amp;rn, hex);  /* integral part */</span>
<span class="lineNum">     479 </span><span class="lineCov">         11 :   if (test2(&amp;rn, decp))  /* decimal point? */</span>
<span class="lineNum">     480 </span><span class="lineCov">          3 :     count += readdigits(&amp;rn, hex);  /* fractional part */</span>
<span class="lineNum">     481 </span><span class="lineCov">         11 :   if (count &gt; 0 &amp;&amp; test2(&amp;rn, (hex ? &quot;pP&quot; : &quot;eE&quot;))) {  /* exponent mark? */</span>
<span class="lineNum">     482 </span><span class="lineCov">          1 :     test2(&amp;rn, &quot;-+&quot;);  /* exponent sign */</span>
<span class="lineNum">     483 </span><span class="lineCov">          1 :     readdigits(&amp;rn, 0);  /* exponent digits */</span>
<span class="lineNum">     484 </span>            :   }
<span class="lineNum">     485 </span><span class="lineCov">         11 :   ungetc(rn.c, rn.f);  /* unread look-ahead char */</span>
<span class="lineNum">     486 </span><span class="lineCov">         11 :   l_unlockfile(rn.f);</span>
<span class="lineNum">     487 </span><span class="lineCov">         11 :   rn.buff[rn.n] = '\0';  /* finish string */</span>
<span class="lineNum">     488 </span><span class="lineCov">         11 :   if (lua_stringtonumber(L, rn.buff))  /* is this a valid number? */</span>
<span class="lineNum">     489 </span><span class="lineCov">          8 :     return 1;  /* ok */</span>
<span class="lineNum">     490 </span>            :   else {  /* invalid format */
<span class="lineNum">     491 </span><span class="lineCov">          3 :    lua_pushnil(L);  /* &quot;result&quot; to be removed */</span>
<span class="lineNum">     492 </span><span class="lineCov">          3 :    return 0;  /* read fails */</span>
<span class="lineNum">     493 </span>            :   }
<span class="lineNum">     494 </span>            : }
<span class="lineNum">     495 </span>            : 
<span class="lineNum">     496 </span>            : 
<span class="lineNum">     497 </span><span class="lineCov">          1 : static int test_eof (lua_State *L, FILE *f) {</span>
<span class="lineNum">     498 </span><span class="lineCov">          1 :   int c = getc(f);</span>
<span class="lineNum">     499 </span><span class="lineCov">          1 :   ungetc(c, f);  /* no-op when c == EOF */</span>
<span class="lineNum">     500 </span><span class="lineCov">          1 :   lua_pushliteral(L, &quot;&quot;);</span>
<span class="lineNum">     501 </span><span class="lineCov">          1 :   return (c != EOF);</span>
<span class="lineNum">     502 </span>            : }
<span class="lineNum">     503 </span>            : 
<span class="lineNum">     504 </span>            : 
<span class="lineNum">     505 </span><span class="lineCov">        266 : static int read_line (lua_State *L, FILE *f, int chop) {</span>
<span class="lineNum">     506 </span>            :   luaL_Buffer b;
<span class="lineNum">     507 </span>            :   int c;
<span class="lineNum">     508 </span><span class="lineCov">        266 :   luaL_buffinit(L, &amp;b);</span>
<span class="lineNum">     509 </span>            :   do {  /* may need to read several chunks to get whole line */
<span class="lineNum">     510 </span><span class="lineCov">        266 :     char *buff = luaL_prepbuffer(&amp;b);  /* preallocate buffer space */</span>
<span class="lineNum">     511 </span><span class="lineCov">        266 :     int i = 0;</span>
<span class="lineNum">     512 </span><span class="lineCov">        266 :     l_lockfile(f);  /* no memory errors can happen inside the lock */</span>
<span class="lineNum">     513 </span><span class="lineCov">       9035 :     while (i &lt; LUAL_BUFFERSIZE &amp;&amp; (c = l_getc(f)) != EOF &amp;&amp; c != '\n')</span>
<span class="lineNum">     514 </span><span class="lineCov">       8769 :       buff[i++] = c;  /* read up to end of line or buffer limit */</span>
<span class="lineNum">     515 </span><span class="lineCov">        266 :     l_unlockfile(f);</span>
<span class="lineNum">     516 </span><span class="lineCov">        266 :     luaL_addsize(&amp;b, i);</span>
<span class="lineNum">     517 </span><span class="lineCov">        266 :   } while (c != EOF &amp;&amp; c != '\n');  /* repeat until end of line */</span>
<span class="lineNum">     518 </span><span class="lineCov">        266 :   if (!chop &amp;&amp; c == '\n')  /* want a newline and have one? */</span>
<span class="lineNum">     519 </span><span class="lineCov">          1 :     luaL_addchar(&amp;b, c);  /* add ending newline to result */</span>
<span class="lineNum">     520 </span><span class="lineCov">        266 :   luaL_pushresult(&amp;b);  /* close buffer */</span>
<span class="lineNum">     521 </span>            :   /* return ok if read something (either a newline or something else) */
<span class="lineNum">     522 </span><span class="lineCov">        266 :   return (c == '\n' || lua_rawlen(L, -1) &gt; 0);</span>
<span class="lineNum">     523 </span>            : }
<span class="lineNum">     524 </span>            : 
<span class="lineNum">     525 </span>            : 
<span class="lineNum">     526 </span><span class="lineCov">          2 : static void read_all (lua_State *L, FILE *f) {</span>
<span class="lineNum">     527 </span>            :   size_t nr;
<span class="lineNum">     528 </span>            :   luaL_Buffer b;
<span class="lineNum">     529 </span><span class="lineCov">          2 :   luaL_buffinit(L, &amp;b);</span>
<span class="lineNum">     530 </span>            :   do {  /* read file in chunks of LUAL_BUFFERSIZE bytes */
<span class="lineNum">     531 </span><span class="lineCov">          2 :     char *p = luaL_prepbuffer(&amp;b);</span>
<span class="lineNum">     532 </span><span class="lineCov">          2 :     nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);</span>
<span class="lineNum">     533 </span><span class="lineCov">          2 :     luaL_addsize(&amp;b, nr);</span>
<span class="lineNum">     534 </span><span class="lineCov">          2 :   } while (nr == LUAL_BUFFERSIZE);</span>
<span class="lineNum">     535 </span><span class="lineCov">          2 :   luaL_pushresult(&amp;b);  /* close buffer */</span>
<span class="lineNum">     536 </span><span class="lineCov">          2 : }</span>
<span class="lineNum">     537 </span>            : 
<span class="lineNum">     538 </span>            : 
<span class="lineNum">     539 </span><span class="lineCov">          4 : static int read_chars (lua_State *L, FILE *f, size_t n) {</span>
<span class="lineNum">     540 </span>            :   size_t nr;  /* number of chars actually read */
<span class="lineNum">     541 </span>            :   char *p;
<span class="lineNum">     542 </span>            :   luaL_Buffer b;
<span class="lineNum">     543 </span><span class="lineCov">          4 :   luaL_buffinit(L, &amp;b);</span>
<span class="lineNum">     544 </span><span class="lineCov">          4 :   p = luaL_prepbuffsize(&amp;b, n);  /* prepare buffer to read whole block */</span>
<span class="lineNum">     545 </span><span class="lineCov">          4 :   nr = fread(p, sizeof(char), n, f);  /* try to read 'n' chars */</span>
<span class="lineNum">     546 </span><span class="lineCov">          4 :   luaL_addsize(&amp;b, nr);</span>
<span class="lineNum">     547 </span><span class="lineCov">          4 :   luaL_pushresult(&amp;b);  /* close buffer */</span>
<span class="lineNum">     548 </span><span class="lineCov">          4 :   return (nr &gt; 0);  /* true iff read something */</span>
<span class="lineNum">     549 </span>            : }
<span class="lineNum">     550 </span>            : 
<span class="lineNum">     551 </span>            : 
<span class="lineNum">     552 </span><span class="lineCov">        276 : static int g_read (lua_State *L, FILE *f, int first) {</span>
<span class="lineNum">     553 </span><span class="lineCov">        276 :   int nargs = lua_gettop(L) - 1;</span>
<span class="lineNum">     554 </span>            :   int n, success;
<span class="lineNum">     555 </span><span class="lineCov">        276 :   clearerr(f);</span>
<span class="lineNum">     556 </span><span class="lineCov">        276 :   if (nargs == 0) {  /* no arguments? */</span>
<span class="lineNum">     557 </span><span class="lineCov">        176 :     success = read_line(L, f, 1);</span>
<span class="lineNum">     558 </span><span class="lineCov">        176 :     n = first + 1;  /* to return 1 result */</span>
<span class="lineNum">     559 </span>            :   }
<span class="lineNum">     560 </span>            :   else {
<span class="lineNum">     561 </span>            :     /* ensure stack space for all results and for auxlib's buffer */
<span class="lineNum">     562 </span><span class="lineCov">        100 :     luaL_checkstack(L, nargs+LUA_MINSTACK, &quot;too many arguments&quot;);</span>
<span class="lineNum">     563 </span><span class="lineCov">        100 :     success = 1;</span>
<span class="lineNum">     564 </span><span class="lineCov">        208 :     for (n = first; nargs-- &amp;&amp; success; n++) {</span>
<span class="lineNum">     565 </span><span class="lineCov">        109 :       if (lua_type(L, n) == LUA_TNUMBER) {</span>
<span class="lineNum">     566 </span><span class="lineCov">          5 :         size_t l = (size_t)luaL_checkinteger(L, n);</span>
<span class="lineNum">     567 </span><span class="lineCov">          5 :         success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);</span>
<span class="lineNum">     568 </span>            :       }
<span class="lineNum">     569 </span>            :       else {
<span class="lineNum">     570 </span><span class="lineCov">        104 :         const char *p = luaL_checkstring(L, n);</span>
<span class="lineNum">     571 </span><span class="lineCov">        104 :         if (*p == '*') p++;  /* skip optional '*' (for compatibility) */</span>
<span class="lineNum">     572 </span><span class="lineCov">        104 :         switch (*p) {</span>
<span class="lineNum">     573 </span><span class="lineCov">         11 :           case 'n':  /* number */</span>
<span class="lineNum">     574 </span><span class="lineCov">         11 :             success = read_number(L, f);</span>
<span class="lineNum">     575 </span><span class="lineCov">         11 :             break;</span>
<span class="lineNum">     576 </span><span class="lineCov">         88 :           case 'l':  /* line */</span>
<span class="lineNum">     577 </span><span class="lineCov">         88 :             success = read_line(L, f, 1);</span>
<span class="lineNum">     578 </span><span class="lineCov">         88 :             break;</span>
<span class="lineNum">     579 </span><span class="lineCov">          2 :           case 'L':  /* line with end-of-line */</span>
<span class="lineNum">     580 </span><span class="lineCov">          2 :             success = read_line(L, f, 0);</span>
<span class="lineNum">     581 </span><span class="lineCov">          2 :             break;</span>
<span class="lineNum">     582 </span><span class="lineCov">          2 :           case 'a':  /* file */</span>
<span class="lineNum">     583 </span><span class="lineCov">          2 :             read_all(L, f);  /* read entire file */</span>
<span class="lineNum">     584 </span><span class="lineCov">          2 :             success = 1; /* always success */</span>
<span class="lineNum">     585 </span><span class="lineCov">          2 :             break;</span>
<span class="lineNum">     586 </span><span class="lineCov">          1 :           default:</span>
<span class="lineNum">     587 </span><span class="lineCov">          1 :             return luaL_argerror(L, n, &quot;invalid format&quot;);</span>
<span class="lineNum">     588 </span>            :         }
<span class="lineNum">     589 </span>            :       }
<span class="lineNum">     590 </span>            :     }
<span class="lineNum">     591 </span>            :   }
<span class="lineNum">     592 </span><span class="lineCov">        275 :   if (ferror(f))</span>
<span class="lineNum">     593 </span><span class="lineNoCov">          0 :     return luaL_fileresult(L, 0, NULL);</span>
<span class="lineNum">     594 </span><span class="lineCov">        275 :   if (!success) {</span>
<span class="lineNum">     595 </span><span class="lineCov">         21 :     lua_pop(L, 1);  /* remove last result */</span>
<span class="lineNum">     596 </span><span class="lineCov">         21 :     lua_pushnil(L);  /* push nil instead */</span>
<span class="lineNum">     597 </span>            :   }
<span class="lineNum">     598 </span><span class="lineCov">        275 :   return n - first;</span>
<span class="lineNum">     599 </span>            : }
<span class="lineNum">     600 </span>            : 
<span class="lineNum">     601 </span>            : 
<span class="lineNum">     602 </span><span class="lineCov">          5 : static int io_read (lua_State *L) {</span>
<span class="lineNum">     603 </span><span class="lineCov">          5 :   return g_read(L, getiofile(L, IO_INPUT), 1);</span>
<span class="lineNum">     604 </span>            : }
<span class="lineNum">     605 </span>            : 
<span class="lineNum">     606 </span>            : 
<span class="lineNum">     607 </span><span class="lineCov">         98 : static int f_read (lua_State *L) {</span>
<span class="lineNum">     608 </span><span class="lineCov">         98 :   return g_read(L, tofile(L), 2);</span>
<span class="lineNum">     609 </span>            : }
<span class="lineNum">     610 </span>            : 
<span class="lineNum">     611 </span>            : 
<span class="lineNum">     612 </span>            : /*
<span class="lineNum">     613 </span>            : ** Iteration function for 'lines'.
<span class="lineNum">     614 </span>            : */
<span class="lineNum">     615 </span><span class="lineCov">        174 : static int io_readline (lua_State *L) {</span>
<span class="lineNum">     616 </span><span class="lineCov">        174 :   LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));</span>
<span class="lineNum">     617 </span>            :   int i;
<span class="lineNum">     618 </span><span class="lineCov">        174 :   int n = (int)lua_tointeger(L, lua_upvalueindex(2));</span>
<span class="lineNum">     619 </span><span class="lineCov">        174 :   if (isclosed(p))  /* file is already closed? */</span>
<span class="lineNum">     620 </span><span class="lineNoCov">          0 :     return luaL_error(L, &quot;file is already closed&quot;);</span>
<span class="lineNum">     621 </span><span class="lineCov">        174 :   lua_settop(L , 1);</span>
<span class="lineNum">     622 </span><span class="lineCov">        174 :   luaL_checkstack(L, n, &quot;too many arguments&quot;);</span>
<span class="lineNum">     623 </span><span class="lineCov">        175 :   for (i = 1; i &lt;= n; i++)  /* push arguments to 'g_read' */</span>
<span class="lineNum">     624 </span><span class="lineCov">          1 :     lua_pushvalue(L, lua_upvalueindex(3 + i));</span>
<span class="lineNum">     625 </span><span class="lineCov">        174 :   n = g_read(L, p-&gt;f, 2);  /* 'n' is number of results */</span>
<span class="lineNum">     626 </span>            :   lua_assert(n &gt; 0);  /* should return at least a nil */
<span class="lineNum">     627 </span><span class="lineCov">        174 :   if (lua_toboolean(L, -n))  /* read at least one value? */</span>
<span class="lineNum">     628 </span><span class="lineCov">        170 :     return n;  /* return them */</span>
<span class="lineNum">     629 </span>            :   else {  /* first result is nil: EOF or error */
<span class="lineNum">     630 </span><span class="lineCov">          4 :     if (n &gt; 1) {  /* is there error information? */</span>
<span class="lineNum">     631 </span>            :       /* 2nd result is error message */
<span class="lineNum">     632 </span><span class="lineNoCov">          0 :       return luaL_error(L, &quot;%s&quot;, lua_tostring(L, -n + 1));</span>
<span class="lineNum">     633 </span>            :     }
<span class="lineNum">     634 </span><span class="lineCov">          4 :     if (lua_toboolean(L, lua_upvalueindex(3))) {  /* generator created file? */</span>
<span class="lineNum">     635 </span><span class="lineCov">          1 :       lua_settop(L, 0);  /* clear stack */</span>
<span class="lineNum">     636 </span><span class="lineCov">          1 :       lua_pushvalue(L, lua_upvalueindex(1));  /* push file at index 1 */</span>
<span class="lineNum">     637 </span><span class="lineCov">          1 :       aux_close(L);  /* close it */</span>
<span class="lineNum">     638 </span>            :     }
<span class="lineNum">     639 </span><span class="lineCov">          4 :     return 0;</span>
<span class="lineNum">     640 </span>            :   }
<span class="lineNum">     641 </span>            : }
<span class="lineNum">     642 </span>            : 
<span class="lineNum">     643 </span>            : /* }====================================================== */
<span class="lineNum">     644 </span>            : 
<span class="lineNum">     645 </span>            : 
<span class="lineNum">     646 </span><span class="lineCov">         36 : static int g_write (lua_State *L, FILE *f, int arg) {</span>
<span class="lineNum">     647 </span><span class="lineCov">         36 :   int nargs = lua_gettop(L) - arg;</span>
<span class="lineNum">     648 </span><span class="lineCov">         36 :   int status = 1;</span>
<span class="lineNum">     649 </span><span class="lineCov">         73 :   for (; nargs--; arg++) {</span>
<span class="lineNum">     650 </span><span class="lineCov">         37 :     if (lua_type(L, arg) == LUA_TNUMBER) {</span>
<span class="lineNum">     651 </span>            :       /* optimization: could be done exactly as for strings */
<span class="lineNum">     652 </span><span class="lineCov">          1 :       int len = lua_isinteger(L, arg)</span>
<span class="lineNum">     653 </span><span class="lineCov">          1 :                 ? fprintf(f, LUA_INTEGER_FMT,</span>
<span class="lineNum">     654 </span><span class="lineCov">          1 :                              (LUAI_UACINT)lua_tointeger(L, arg))</span>
<span class="lineNum">     655 </span><span class="lineCov">          1 :                 : fprintf(f, LUA_NUMBER_FMT,</span>
<span class="lineNum">     656 </span><span class="lineNoCov">          0 :                              (LUAI_UACNUMBER)lua_tonumber(L, arg));</span>
<span class="lineNum">     657 </span><span class="lineCov">          1 :       status = status &amp;&amp; (len &gt; 0);</span>
<span class="lineNum">     658 </span>            :     }
<span class="lineNum">     659 </span>            :     else {
<span class="lineNum">     660 </span>            :       size_t l;
<span class="lineNum">     661 </span><span class="lineCov">         36 :       const char *s = luaL_checklstring(L, arg, &amp;l);</span>
<span class="lineNum">     662 </span><span class="lineCov">         36 :       status = status &amp;&amp; (fwrite(s, sizeof(char), l, f) == l);</span>
<span class="lineNum">     663 </span>            :     }
<span class="lineNum">     664 </span>            :   }
<span class="lineNum">     665 </span><span class="lineCov">         36 :   if (status) return 1;  /* file handle already on stack top */</span>
<span class="lineNum">     666 </span><span class="lineNoCov">          0 :   else return luaL_fileresult(L, status, NULL);</span>
<span class="lineNum">     667 </span>            : }
<span class="lineNum">     668 </span>            : 
<span class="lineNum">     669 </span>            : 
<span class="lineNum">     670 </span><span class="lineCov">          2 : static int io_write (lua_State *L) {</span>
<span class="lineNum">     671 </span><span class="lineCov">          2 :   return g_write(L, getiofile(L, IO_OUTPUT), 1);</span>
<span class="lineNum">     672 </span>            : }
<span class="lineNum">     673 </span>            : 
<span class="lineNum">     674 </span>            : 
<span class="lineNum">     675 </span><span class="lineCov">         35 : static int f_write (lua_State *L) {</span>
<span class="lineNum">     676 </span><span class="lineCov">         35 :   FILE *f = tofile(L);</span>
<span class="lineNum">     677 </span><span class="lineCov">         34 :   lua_pushvalue(L, 1);  /* push file at the stack top (to be returned) */</span>
<span class="lineNum">     678 </span><span class="lineCov">         34 :   return g_write(L, f, 2);</span>
<span class="lineNum">     679 </span>            : }
<span class="lineNum">     680 </span>            : 
<span class="lineNum">     681 </span>            : 
<span class="lineNum">     682 </span><span class="lineCov">          3 : static int f_seek (lua_State *L) {</span>
<span class="lineNum">     683 </span>            :   static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
<span class="lineNum">     684 </span>            :   static const char *const modenames[] = {&quot;set&quot;, &quot;cur&quot;, &quot;end&quot;, NULL};
<span class="lineNum">     685 </span><span class="lineCov">          3 :   FILE *f = tofile(L);</span>
<span class="lineNum">     686 </span><span class="lineCov">          2 :   int op = luaL_checkoption(L, 2, &quot;cur&quot;, modenames);</span>
<span class="lineNum">     687 </span><span class="lineCov">          1 :   lua_Integer p3 = luaL_optinteger(L, 3, 0);</span>
<span class="lineNum">     688 </span><span class="lineCov">          1 :   l_seeknum offset = (l_seeknum)p3;</span>
<span class="lineNum">     689 </span><span class="lineCov">          1 :   luaL_argcheck(L, (lua_Integer)offset == p3, 3,</span>
<span class="lineNum">     690 </span>            :                   &quot;not an integer in proper range&quot;);
<span class="lineNum">     691 </span><span class="lineCov">          1 :   op = l_fseek(f, offset, mode[op]);</span>
<span class="lineNum">     692 </span><span class="lineCov">          1 :   if (op)</span>
<span class="lineNum">     693 </span><span class="lineNoCov">          0 :     return luaL_fileresult(L, 0, NULL);  /* error */</span>
<span class="lineNum">     694 </span>            :   else {
<span class="lineNum">     695 </span><span class="lineCov">          1 :     lua_pushinteger(L, (lua_Integer)l_ftell(f));</span>
<span class="lineNum">     696 </span><span class="lineCov">          1 :     return 1;</span>
<span class="lineNum">     697 </span>            :   }
<span class="lineNum">     698 </span>            : }
<span class="lineNum">     699 </span>            : 
<span class="lineNum">     700 </span>            : 
<span class="lineNum">     701 </span><span class="lineCov">          3 : static int f_setvbuf (lua_State *L) {</span>
<span class="lineNum">     702 </span>            :   static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
<span class="lineNum">     703 </span>            :   static const char *const modenames[] = {&quot;no&quot;, &quot;full&quot;, &quot;line&quot;, NULL};
<span class="lineNum">     704 </span><span class="lineCov">          3 :   FILE *f = tofile(L);</span>
<span class="lineNum">     705 </span><span class="lineCov">          3 :   int op = luaL_checkoption(L, 2, NULL, modenames);</span>
<span class="lineNum">     706 </span><span class="lineCov">          3 :   lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);</span>
<span class="lineNum">     707 </span><span class="lineCov">          3 :   int res = setvbuf(f, NULL, mode[op], (size_t)sz);</span>
<span class="lineNum">     708 </span><span class="lineCov">          3 :   return luaL_fileresult(L, res == 0, NULL);</span>
<span class="lineNum">     709 </span>            : }
<span class="lineNum">     710 </span>            : 
<span class="lineNum">     711 </span>            : 
<span class="lineNum">     712 </span>            : 
<span class="lineNum">     713 </span><span class="lineCov">          1 : static int io_flush (lua_State *L) {</span>
<span class="lineNum">     714 </span><span class="lineCov">          1 :   return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);</span>
<span class="lineNum">     715 </span>            : }
<span class="lineNum">     716 </span>            : 
<span class="lineNum">     717 </span>            : 
<span class="lineNum">     718 </span><span class="lineCov">          2 : static int f_flush (lua_State *L) {</span>
<span class="lineNum">     719 </span><span class="lineCov">          2 :   return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);</span>
<span class="lineNum">     720 </span>            : }
<span class="lineNum">     721 </span>            : 
<span class="lineNum">     722 </span>            : 
<span class="lineNum">     723 </span>            : /*
<span class="lineNum">     724 </span>            : ** functions for 'io' library
<span class="lineNum">     725 </span>            : */
<span class="lineNum">     726 </span>            : static const luaL_Reg iolib[] = {
<span class="lineNum">     727 </span>            :   {&quot;close&quot;, io_close},
<span class="lineNum">     728 </span>            :   {&quot;flush&quot;, io_flush},
<span class="lineNum">     729 </span>            :   {&quot;input&quot;, io_input},
<span class="lineNum">     730 </span>            :   {&quot;lines&quot;, io_lines},
<span class="lineNum">     731 </span>            :   {&quot;open&quot;, io_open},
<span class="lineNum">     732 </span>            :   {&quot;output&quot;, io_output},
<span class="lineNum">     733 </span>            :   {&quot;popen&quot;, io_popen},
<span class="lineNum">     734 </span>            :   {&quot;read&quot;, io_read},
<span class="lineNum">     735 </span>            :   {&quot;tmpfile&quot;, io_tmpfile},
<span class="lineNum">     736 </span>            :   {&quot;type&quot;, io_type},
<span class="lineNum">     737 </span>            :   {&quot;write&quot;, io_write},
<span class="lineNum">     738 </span>            :   {NULL, NULL}
<span class="lineNum">     739 </span>            : };
<span class="lineNum">     740 </span>            : 
<span class="lineNum">     741 </span>            : 
<span class="lineNum">     742 </span>            : /*
<span class="lineNum">     743 </span>            : ** methods for file handles
<span class="lineNum">     744 </span>            : */
<span class="lineNum">     745 </span>            : static const luaL_Reg flib[] = {
<span class="lineNum">     746 </span>            :   {&quot;close&quot;, f_close},
<span class="lineNum">     747 </span>            :   {&quot;flush&quot;, f_flush},
<span class="lineNum">     748 </span>            :   {&quot;lines&quot;, f_lines},
<span class="lineNum">     749 </span>            :   {&quot;read&quot;, f_read},
<span class="lineNum">     750 </span>            :   {&quot;seek&quot;, f_seek},
<span class="lineNum">     751 </span>            :   {&quot;setvbuf&quot;, f_setvbuf},
<span class="lineNum">     752 </span>            :   {&quot;write&quot;, f_write},
<span class="lineNum">     753 </span>            :   {&quot;__gc&quot;, f_gc},
<span class="lineNum">     754 </span>            :   {&quot;__close&quot;, f_gc},
<span class="lineNum">     755 </span>            :   {&quot;__tostring&quot;, f_tostring},
<span class="lineNum">     756 </span>            :   {NULL, NULL}
<span class="lineNum">     757 </span>            : };
<span class="lineNum">     758 </span>            : 
<span class="lineNum">     759 </span>            : 
<span class="lineNum">     760 </span><span class="lineCov">         89 : static void createmeta (lua_State *L) {</span>
<span class="lineNum">     761 </span><span class="lineCov">         89 :   luaL_newmetatable(L, LUA_FILEHANDLE);  /* create metatable for file handles */</span>
<span class="lineNum">     762 </span><span class="lineCov">         89 :   lua_pushvalue(L, -1);  /* push metatable */</span>
<span class="lineNum">     763 </span><span class="lineCov">         89 :   lua_setfield(L, -2, &quot;__index&quot;);  /* metatable.__index = metatable */</span>
<span class="lineNum">     764 </span><span class="lineCov">         89 :   luaL_setfuncs(L, flib, 0);  /* add file methods to new metatable */</span>
<span class="lineNum">     765 </span><span class="lineCov">         89 :   lua_pop(L, 1);  /* pop new metatable */</span>
<span class="lineNum">     766 </span><span class="lineCov">         89 : }</span>
<span class="lineNum">     767 </span>            : 
<span class="lineNum">     768 </span>            : 
<span class="lineNum">     769 </span>            : /*
<span class="lineNum">     770 </span>            : ** function to (not) close the standard files stdin, stdout, and stderr
<span class="lineNum">     771 </span>            : */
<span class="lineNum">     772 </span><span class="lineCov">        242 : static int io_noclose (lua_State *L) {</span>
<span class="lineNum">     773 </span><span class="lineCov">        242 :   LStream *p = tolstream(L);</span>
<span class="lineNum">     774 </span><span class="lineCov">        242 :   p-&gt;closef = &amp;io_noclose;  /* keep file opened */</span>
<span class="lineNum">     775 </span><span class="lineCov">        242 :   lua_pushnil(L);</span>
<span class="lineNum">     776 </span><span class="lineCov">        242 :   lua_pushliteral(L, &quot;cannot close standard file&quot;);</span>
<span class="lineNum">     777 </span><span class="lineCov">        242 :   return 2;</span>
<span class="lineNum">     778 </span>            : }
<span class="lineNum">     779 </span>            : 
<span class="lineNum">     780 </span>            : 
<span class="lineNum">     781 </span><span class="lineCov">        267 : static void createstdfile (lua_State *L, FILE *f, const char *k,</span>
<span class="lineNum">     782 </span>            :                            const char *fname) {
<span class="lineNum">     783 </span><span class="lineCov">        267 :   LStream *p = newprefile(L);</span>
<span class="lineNum">     784 </span><span class="lineCov">        267 :   p-&gt;f = f;</span>
<span class="lineNum">     785 </span><span class="lineCov">        267 :   p-&gt;closef = &amp;io_noclose;</span>
<span class="lineNum">     786 </span><span class="lineCov">        267 :   if (k != NULL) {</span>
<span class="lineNum">     787 </span><span class="lineCov">        178 :     lua_pushvalue(L, -1);</span>
<span class="lineNum">     788 </span><span class="lineCov">        178 :     lua_setfield(L, LUA_REGISTRYINDEX, k);  /* add file to registry */</span>
<span class="lineNum">     789 </span>            :   }
<span class="lineNum">     790 </span><span class="lineCov">        267 :   lua_setfield(L, -2, fname);  /* add file to module */</span>
<span class="lineNum">     791 </span><span class="lineCov">        267 : }</span>
<span class="lineNum">     792 </span>            : 
<span class="lineNum">     793 </span>            : 
<span class="lineNum">     794 </span><span class="lineCov">         89 : LUAMOD_API int luaopen_io (lua_State *L) {</span>
<span class="lineNum">     795 </span><span class="lineCov">         89 :   luaL_newlib(L, iolib);  /* new module */</span>
<span class="lineNum">     796 </span><span class="lineCov">         89 :   createmeta(L);</span>
<span class="lineNum">     797 </span>            :   /* create (and set) default files */
<span class="lineNum">     798 </span><span class="lineCov">         89 :   createstdfile(L, stdin, IO_INPUT, &quot;stdin&quot;);</span>
<span class="lineNum">     799 </span><span class="lineCov">         89 :   createstdfile(L, stdout, IO_OUTPUT, &quot;stdout&quot;);</span>
<span class="lineNum">     800 </span><span class="lineCov">         89 :   createstdfile(L, stderr, NULL, &quot;stderr&quot;);</span>
<span class="lineNum">     801 </span><span class="lineCov">         89 :   return 1;</span>
<span class="lineNum">     802 </span>            : }
<span class="lineNum">     803 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
